﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;
using System.IO;
using System.Threading;
using BizMock.Adapter.Runtime;
using Microsoft.XLANGs.BaseTypes;
using System.Xml.Serialization;

namespace BizMock.Tests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>    
    [TestClass]    
    public class BizMockeryTests
    {
        public BizMockeryTests()
        {
           
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize() 
        {
            BizMockery.Reset(true);

        }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion       

        private int CountBizMockFilesByCriteria(string criteria)
        {
            return Directory.GetFiles("c:\\programdata\\bizmock").Where<string>(x => x.Contains(criteria)).Count<string>();
        }
    
        [TestMethod]
        public void VerifyEventLog()
        {
            System.Diagnostics.EventLog.WriteEntry("BizMock", "verifyEventLog test");
            Expect.EventLogSource("BizMock").Verify("verifyEventLog test");
        }

        [TestMethod]
        public void VerifyNoEventLog()
        {
            try
            {
                Expect.EventLogSource("Inexistent Source").Verify(Guid.NewGuid().ToString());
            }
            catch (Exception e)
            {
                Assert.AreEqual("event not found", e.Message);
            }
        }

        [TestMethod]
        [DeploymentItem("MessageInstances")]
        public void VerifyFileInstance()
        {
            BizMockMessage msg = new BizMockMessage("simpleschema_input.xml");
            XmlMessageVerifier msgVrf = new XmlMessageVerifier();
            msgVrf.ExpectedValue("/*[local-name()='Root' and namespace-uri()='http://SimpleSchema']/*[local-name()='Field' and namespace-uri()='']", "X");
            Expect.Message(msg).Verify(msgVrf);
        }

        [TestMethod]
        [DeploymentItem("MessageInstances")]
        //Testing a map
        public void TransformFileInstance()
        {            
            XmlMessageVerifier msgVrf = new XmlMessageVerifier();
            msgVrf.ExpectedValue("/*[local-name()='Root' and namespace-uri()='http://SimpleSchema2']/*[local-name()='Field' and namespace-uri()='']", "x");
            Expect.Message("simpleschema_input.xml").TransformedWith( new BizMock.Samples.Map1() ).Verify(msgVrf);
        }

        [TestMethod]
        [DeploymentItem("MessageInstances")]
        public void VerifyDatabase()
        {
            Expect.DBResult("<dbRslt><Orders C=\"0\" /></dbRslt>").ForQuery("select count(*) as C from Orders for xml auto, TYPE,ROOT('dbRslt')").AtDatabase("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BizMockTestDB;Data Source=.\\SQLEXPRESS");            
        }

        [TestMethod]
        [DeploymentItem("MessageInstances")]
        public void ExpectFiles()
        {
            XmlMessageVerifier msgVrf = new XmlMessageVerifier();
            msgVrf.ExpectedValue("/*[local-name()='Root' and namespace-uri()='http://SimpleSchema2']/*[local-name()='Field' and namespace-uri()='']", "Y");
            Expect.One.File.Matching("simpleschema2_input.xml").At(".").Verify(msgVrf);
        }

        [TestMethod]
        [DeploymentItem("MessageInstances")]
        public void MapVerifierTest()
        {
            XmlMessageVerifier msgVrf = new XmlMessageVerifier();
            msgVrf.ExpectedValue("/*[local-name()='Root' and namespace-uri()='http://SimpleSchema2']/*[local-name()='Field' and namespace-uri()='']", "x");
            Assert.IsTrue(MapVerifier.Verify("simpleschema_input.xml", new BizMock.Samples.Map1(), msgVrf));
        }

        [TestMethod]
        [DeploymentItem("MessageInstances")]
        public void MessageVerifierTest()
        {
            XmlMessageVerifier msgVrf = new XmlMessageVerifier();
            msgVrf.ExpectedValue("/*[local-name()='Root' and namespace-uri()='http://SimpleSchema2']/*[local-name()='Field' and namespace-uri()='']", "Y");
            Assert.IsTrue(MessageVerifier.Verify("simpleschema2_input.xml", msgVrf));
        }        
         
    }   

}